#include "Stdafx.h"
#include "DropProcessCfg.h"

bool CDropProcessCfg::Init()
{
	return LoadXML();
}

bool CDropProcessCfg::ReLoad()
{
	return LoadXML();
}


bool CDropProcessCfg::LoadXML()
{
	XMLBase mapBase;

	if (!mapBase.Load(DROPPROCESS_FILEPATH))	{
		printf("Error,Load %s !\n", DROPPROCESS_FILEPATH);
		return false;
	}

	m_Processes.clear();
	for ( TiXmlElement* pElement = mapBase.Root()->FirstChildElement("process"); NULL != pElement; 
		pElement = pElement->NextSiblingElement("process"))	{
			Process stProcess;
			if ( !ParseProcess(pElement, stProcess) )	{
				return false;
			}
			m_Processes[stProcess.nId] = stProcess;
	}
	return true;
}

bool CDropProcessCfg::ParseProcess(TiXmlElement* pElemen, Process& stProcess)
{
	stProcess.nId = atoi(pElemen->FirstChildElement("id")->GetText());
	stProcess.eType = (PROCESS_TYPE)atoi(pElemen->FirstChildElement("type")->GetText());

	for ( TiXmlElement* pDtataElement = pElemen->FirstChildElement("data"); NULL != pDtataElement; 
		pDtataElement = pDtataElement->NextSiblingElement("data"))	{
			ProcessData stProcessData;
			if ( !ParseProcessData(pDtataElement, stProcessData) )	{
				return false;
			}
			stProcess.Datas.push_back(stProcessData);
	}
	return true;
}

bool CDropProcessCfg::ParseProcessData(TiXmlElement* pElemen, ProcessData& stProcessData)
{
	TiXmlElement* pDtataElement = NULL;
	pDtataElement = pElemen->FirstChildElement("datatype");
	if(pDtataElement)
		stProcessData.eType = (PROCESS_EVENTTYPE)atoi(pDtataElement->GetText());
	pDtataElement = pElemen->FirstChildElement("param1");
	if(pDtataElement)
		stProcessData.nParam1 = (PROCESS_EVENTTYPE)atoi(pDtataElement->GetText());
	pDtataElement = pElemen->FirstChildElement("param2");
	if(pDtataElement)
		stProcessData.nParam2 = (PROCESS_EVENTTYPE)atoi(pDtataElement->GetText());
	return true;
}

bool CDropProcessCfg::GetProcess(int nId, Process& stProcess)
{
	PROCESSMAP::iterator it = m_Processes.find(nId);
	if ( it == m_Processes.end())
	{
		return false;
	}
	stProcess = it->second;
	return true;
}